package com.teatime.client.user;

import java.io.UnsupportedEncodingException;

import android.content.Intent;
import android.view.View;

import com.teatime.client.Constants;
import com.teatime.client.R;
import com.teatime.client.TLog;
import com.teatime.client.TeaTimeUtils;
import com.teatime.client.activity.TActivity;
import com.teatime.client.net.http.HttpRequestParams;
import com.teatime.client.net.http.THttpClient;
import com.teatime.client.net.http.THttpClientListener;

public class Register1Activity extends TActivity implements THttpClientListener {

	final private String Action = "/videos/reg1.php";
	
	private String mMobile;
	@Override
	public int getContentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_register1;
	}

	public void getVeriCode(View v) {
		THttpClient client = new THttpClient(Constants.ServerHost + Action, this, this);
		HttpRequestParams params = new HttpRequestParams();
		String mobile = getTextFromEdit(R.id.txt_mobile);
		if (!TeaTimeUtils.mobileValidate(mobile)) {
			showErrMsg(R.string.err_invalid_mobile);
			return;
		}
		
		params.add("mobile", mobile);
		try {
			client.post(params);
			mMobile = mobile;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			showErrMsg(R.string.err_internet);
			TLog.e("failed in httpclient post, " + e.toString());
		}
		
	}

	@Override
	public void onGetResponse(int statusCode, String response) {
		TLog.v("response status code: " + statusCode);
		if (statusCode == 200) {
			if (response.startsWith("ok")) {
				Intent i = activityIntent(".user.Register2Activity");
				i.putExtra("regMobile", mMobile);
				startActivity(i);
				finish();
			}
			else {
				showErrMsg(response.trim());
			}
		}
		else {
			showErrMsg(R.string.err_server_exception);
		}
	}

	@Override
	public void onHttpError(String errmsg) {
		// TODO Auto-generated method stub
		showErrMsg(R.string.err_internet);
		TLog.e(errmsg);
		
	}
}
